using RealtimeCollabGame.Application.Commands;
using RealtimeCollabGame.Application.Interfaces;
using RealtimeCollabGame.Domain.Exceptions;

namespace RealtimeCollabGame.Application.Handlers;

public class DeleteGameCommandHandler
{
    private readonly IGameRepository _gameRepository;

    public DeleteGameCommandHandler(IGameRepository gameRepository)
    {
        _gameRepository = gameRepository;
    }

    public async Task HandleAsync(DeleteGameCommand command)
    {
        var game = await _gameRepository.GetByIdAsync(command.GameId);
        if (game == null)
        {
            throw new DomainException("游戏不存在");
        }

        // 检查游戏状态，只有等待中的游戏才能删除
        if (game.Status != Domain.Enums.GameStatus.Waiting)
        {
            throw new DomainException("只能删除等待中的游戏");
        }

        await _gameRepository.DeleteAsync(command.GameId);
    }
}